﻿using ControlzEx.Theming;
using Notifications.Wpf;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using VideoTools.vo;
using Application = System.Windows.Application;
using Path = System.Windows.Shapes.Path;
namespace VideoTools
{
    public sealed class AppManager : NotifyPropertyBase, IDisposable
    {
        private static volatile AppManager _instance;
        private static object _syncRoot = new object();
        public readonly NotificationManager NotificationManager = new NotificationManager();
        public static volatile bool EXIT_APP = false;
        public static AppManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new AppManager();
                        }

                    }
                }

                return _instance;
            }
        }

        private AppManager()
        {

        }
#if DEBUG
        public Visibility DebugVisibility => Visibility.Visible;
#else
        public Visibility DebugVisibility => Visibility.Collapsed;
#endif
        private bool _isInitialized = false;

        public bool IsInitialized
        {
            get => _isInitialized;
            private set => SetProperty(ref _isInitialized, value);
        }
        public void Dispose()
        {
            if (!IsInitialized)
                return;

            IsInitialized = false;
        }
        private List<MenuVo> _SubtitleMenus = new List<MenuVo>()
        {
            new MenuVo("调整时间轴", "/SubtitleView/TimelinePage.xaml", (Path)Application.Current.Resources["Icon_d1"], "调整字幕时间轴"),
            new MenuVo("字幕合并", "/SubtitleView/MargeSubtitlePage.xaml", (Path)Application.Current.Resources["Icon_d2"], "合并多个字幕文件"),
            new MenuVo("双语字幕分离", "/SubtitleView/SplitSubtitlePage.xaml", (Path)Application.Current.Resources["Icon_d3"], "将双语字幕分离为单独的字幕文件"),
            new MenuVo("sup字幕识别", "/SubtitleView/OcrSupSubtitlePage.xaml", (Path)Application.Current.Resources["Icon_d4"], "识别sup格式的字幕文件"),
            new MenuVo("时间轴调整", "/SubtitleView/AjustTimelinePage.xaml", (Path)Application.Current.Resources["Icon_d5"], "调整字幕时间轴"),
            new MenuVo("字幕翻译", "/SubtitleView/TranslateSubtitlePage.xaml", (Path)Application.Current.Resources["Icon_d6"], "翻译字幕文件"),
            new MenuVo("字幕搜索", "/SubtitleView/SearchSubtitlePage.xaml", (Path)Application.Current.Resources["Icon_d7"], "搜索字幕内容")

        };
        public List<MenuVo> SubtitleMenus => _SubtitleMenus;


        public List<MenuVo> VideoMenus => _videoMenus;

        private List<MenuVo> _videoMenus = new List<MenuVo>()
        {
            new MenuVo("删除水印", "/VideoView/RemoveWatermarkPage.xaml", (Path)Application.Current.Resources["Icon_d1"], "删除视频中的水印"),
            new MenuVo("删除水印2", "/VideoView/RemoveWatermark2Page.xaml", (Path)Application.Current.Resources["Icon_d2"], "删除视频中的水印2"),
            new MenuVo("视频截取", "/VideoView/SplitVideoPage.xaml", (Path)Application.Current.Resources["Icon_d3"], "截取视频片段"),
            new MenuVo("分辨率调整", "/VideoView/ScaleVideoPage.xaml", (Path)Application.Current.Resources["Icon_d4"], "调整视频分辨率"),
            new MenuVo("M3U8合并", "/VideoView/MargeM3U8Page.xaml", (Path)Application.Current.Resources["Icon_d5"], "合并M3U8视频流"),
            new MenuVo("M3U8下载", "/VideoView/DownloadM3u8Page.xaml", (Path)Application.Current.Resources["Icon_d6"], "下载M3U8视频流"),
            new MenuVo("FFmpeg处理", "/VideoView/FFmpegPage.xaml", (Path)Application.Current.Resources["Icon_d7"], "使用FFmpeg处理视频文件"),
            new MenuVo("FFmpeg批处理", "/VideoView/BatchFFmpegPage.xaml", (Path)Application.Current.Resources["Icon_d8"], "批量处理文件中FFmpeg命令"),
            new MenuVo("ASCII处理", "/VideoView/AsciiVideoPage.xaml", (Path)Application.Current.Resources["Icon_d9"], "将视频转换为ASCII艺术"),
            new MenuVo("编码转换", "/VideoView/ConvertCodePage.xaml", (Path)Application.Current.Resources["Icon_d10"], "转换视频编码格式")
        };
            
            
         

        private Dictionary<string, Page> _subtitlePages;

        public Dictionary<string, Page> SubtitlePages
        {
            get => _subtitlePages;
            private set => SetProperty(ref _subtitlePages, value);
        }

        private Dictionary<string,Page> _videoPages;

        public Dictionary<string, Page> VideoPages
        {
            get => _videoPages;
            private set => SetProperty(ref _videoPages, value);
        }

        private double _PageWidth;

        public double PageWidth
        {
            get => _PageWidth;
            set => SetProperty(ref _PageWidth, value);
        }
        private double _PageHeight;

        public double PageHeight
        {
            get => _PageHeight;
            set => SetProperty(ref _PageHeight, value);
        }

        private bool _SettingsFlyoutOpened = false;

        public bool SettingsFlyoutOpened
        {
            get => _SettingsFlyoutOpened;
            set => SetProperty(ref _SettingsFlyoutOpened, value);
        }

        private ObservableCollection<ThemeColor> _ThemeColors = new ObservableCollection<ThemeColor>();
        public ObservableCollection<ThemeColor> ThemeColors
        {
            get => _ThemeColors;
            set => SetProperty(ref _ThemeColors, value);

        }

        private ObservableCollection<AccentColorMenuData> _AccentColors = new ObservableCollection<AccentColorMenuData>();

        public ObservableCollection<AccentColorMenuData> AccentColors
        {
            get => _AccentColors;
            private set => SetProperty(ref _AccentColors, value);

        }
        private SolidColorBrush _Background = new SolidColorBrush(Colors.AliceBlue);

        public SolidColorBrush BackgroundColor
        {
            get => _Background;
            set => SetProperty(ref _Background, value);

        }

        private ObservableCollection<ThemeColor> _Backgrounds = new ObservableCollection<ThemeColor>();

        public ObservableCollection<ThemeColor> BackgroundColors
        {
            get => _Backgrounds;
            set => SetProperty(ref _Backgrounds, value);

        }

        public static void InitCacheDirectory()
        {
            DirectoryInfo di = null;
            di = new DirectoryInfo(AppConstants.CachePath);
            if (di.Exists)
            {
                di.Delete(true);
            }
            di.Create();
            di = new DirectoryInfo(AppConstants.InPath);
            if (di.Exists)
            {
                di.Delete(true);
            }
            di.Create();
            di = new DirectoryInfo(AppConstants.OutPath);
            if (di.Exists)
            {
                di.Delete(true);
            }
            di.Create();

        }
        private void DeleteCache()
        {
            string rootPath = Directory.GetCurrentDirectory();
            DirectoryInfo root = new DirectoryInfo(rootPath);
            FileInfo[] files = root.GetFiles();
            foreach (FileInfo f in files)
            {
                string fName = f.Name;
                if (fName.StartsWith(AppConstants.ImageFileStart) || fName.StartsWith(AppConstants.LogFileStart))
                {
                    File.Delete("./" + fName);
                }
            }
        }
        public void Initialize()
        {
            InitCacheDirectory();
            DeleteCache();
            foreach(AccentColorMenuData data in ThemeManager.Current.Themes
                                            .GroupBy(x => x.ColorScheme)
                                            .OrderBy(a => a.Key)
                                            .Select(a => new AccentColorMenuData { Name = a.Key, ColorBrush = a.First().ShowcaseBrush })
                                            .ToList())
            {
                AccentColors.Add(data);
            }
        }
    }
}
